﻿using System;
using System.Collections.Generic;
using System.Drawing;

using NGE;

namespace MH.MENU
{
    public class CHRBag : Panelbox
    {
        public CHRBag(Game g)
            : base(g)
        {
            Name = "人物背包";


            desp = new Textbox();
            desp.Text = "";
            desp.Location = new Microsoft.DirectX.Vector3(160, 20, 0);
            Elements.Add(desp);

            pagelabe = new Textbox();
            pagelabe.Text = "1/1";
            pagelabe.Location = new Microsoft.DirectX.Vector3(0, 200, 0);
            Elements.Add(pagelabe);

            page_index = 0;

            this.OnPanelboxOpen += new MenuEvent(Bag_OnPanelboxOpen);
            this.OnPanelboxRefresh += new MenuEvent(Bag_OnPanelboxRefresh);
        }


        void bb_OnButtonboxEvent(object sender, object arg)
        {
            if (props.Count==0)
            {
                return;
            }

            Buttonbox me = (Buttonbox)sender;
            int i = me.StopIndex;

            Prop prop = props[prop_base_index + i];

            if ((ButtonboxState)arg == ButtonboxState.Focus)
            {
                desp.Text = prop.Description;
            }
            else if ((ButtonboxState)arg == ButtonboxState.Up)
            {
                scriptCOM.AS("menu open 背包-操作 要怎样？");
                scriptCOM.AS("block menu_return # null");

                scriptCOM.AS("if menu_return = 1");
                string str = sceneCOM.GetCHR(cn).Bag[i].Script;
                if (str != string.Empty)
                {
                    scriptCOM.AS("then " + cn + " " + str);
                }
                scriptCOM.AS("then " + cn + " use " + i);
                scriptCOM.AS("then menu refresh " + Name + " " + cn);

                scriptCOM.AS("if menu_return = 3");
                scriptCOM.AS("then " + cn + " drop " + i);
                scriptCOM.AS("then menu refresh " + Name + " " + cn);
            }
        }

        void Bag_OnPanelboxRefresh(object sender, object arg)
        {
            cn = (string)arg;
            refresh();
        }

        void Bag_OnPanelboxOpen(object sender, object arg)
        {
            cn = (string)arg;
            page_index = 1;
            refresh();
        }

        void refresh()
        {
            props = sceneCOM.GetCHR(cn).Bag;


            page_count = props.Count / page_max_count;
            if (props.Count % page_max_count > 0)
            {
                page_count++;
            }
            if (page_count < 1)
            {
                page_count = 1;
            }

            desp.Text = "";
            scriptCOM.SetValue("menu_return", "null");

            for (int i = 0; i < Buttonboxes.Count; i++)
            {
                Buttonboxes[i].OnButtonboxEvent -= new MenuEvent(bb_OnButtonboxEvent);
            }

            Buttonboxes.Clear();

            pagelabe.Text = " " + page_index + "/" + page_count + "  钱包：" + sceneCOM.GetCHR(cn).G + "G";
            for (int i = 0; i < page_max_count; i++)
            {
                try
                {
                    Prop prop = props[(page_index - 1) * page_max_count + i];
                    Buttonbox bb = new Buttonbox();
                    bb.Textbox.Text = prop.Name;
                    bb.Location = new Microsoft.DirectX.Vector3(20, 20 + i * 20, 0);
                    bb.OnButtonboxEvent += new MenuEvent(bb_OnButtonboxEvent);
                    Buttonboxes.Add(bb);
                }
                catch
                {
                    break;
                }
            }
            ResetButtonboxIndex();
        }

        SceneComponent sceneCOM
        {
            get
            {
                return ((SceneComponent)game.GetComponent("SceneCOM"));
            }
        }
        ScriptComponent scriptCOM
        {
            get
            {
                return ((ScriptComponent)game.GetComponent("ScriptCOM"));
            }
        }
        Textbox desp;
        Textbox pagelabe;
        string cn;

        List<Prop> props;
        int page_index;
        int page_count;
        int page_max_count = 8;
        int prop_base_index
        {
            get
            {
                return (page_index - 1) * page_max_count;
            }
        }

        protected override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            if (ks == KeyboardState.KeyDown)
            {
                if (key == game.Option.KeyL)
                {
                    page_index--;
                    if (page_index < 1)
                    {
                        page_index = 1;
                    }
                    refresh();
                }
                else if (key == game.Option.KeyR)
                {
                    page_index++;
                    if (page_index > page_count)
                    {
                        page_index = page_count;
                    }
                    refresh();
                }
            }
            base.OnKeyboardEventCode(sender, key, ks);
        }
    }
}
